package com.linjacy.httpproject.util;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.linjacy.httpproject.builder.SqlBuilder;
import com.linjacy.httpproject.exception.SystemErrorException;
import com.mysql.cj.util.StringUtils;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Optional;

/**
 *  Json转为select语句
 */
public class JsonToSelectUtil {

    //多表名中间逗号隔开
    private final static String TABLENAMEKEY = "tableName";
    //连接方式,参照SqlJoinEnum
    private final static String JOINWAY = "JOINWAY";
    //连接条件,例:user.userId = user_detail.userId
    private final static String JOINFIELD = "JOINFIELD";
    //分组信息
    private final static String GROUP = "GROUP";
    //条件
    private final static String CONDITION = "CONDITION";
    //排序
    private final static String ORDER = "ORDER";


    public static String parseJson(JSONObject jsonObject) throws SystemErrorException {
        HashMap<String,Object> map = JSON.parseObject(JSON.toJSONString(jsonObject), HashMap.class);
        Optional.ofNullable(map).orElseThrow(() -> new SystemErrorException("json传入错误"));
        if(!checkTheKey(map.get(TABLENAMEKEY))){
            throw new SystemErrorException(MessageFormat.format("缺乏{0}参数", TABLENAMEKEY));
        }
        String[] splitTableName = map.get(TABLENAMEKEY).toString().split(",");

        if(!checkTheKey(map.get(JOINWAY))){
            throw new SystemErrorException(MessageFormat.format("缺乏{0}参数", JOINWAY));
        }

        if(!checkTheKey(map.get(JOINFIELD))){
            throw new SystemErrorException(MessageFormat.format("缺乏{0}参数", JOINFIELD));
        }

        String[] joinWay = map.get(JsonToSelectUtil.JOINWAY).toString().split(",");
        String[] JoinField = map.get(JsonToSelectUtil.JOINFIELD).toString().split(",");

        if(!checkTheKey(splitTableName, joinWay, JoinField)){
            throw new SystemErrorException("json传入错误");
        }

        String groupInfo = null;
        String conditionInfo = null;
        String orderInfo = null;

        if(checkTheKey(map.get(GROUP))){
            groupInfo = map.get(GROUP).toString();
        }

        if(checkTheKey(map.get(CONDITION))){
            conditionInfo = map.get(CONDITION).toString();
        }

        if(checkTheKey(map.get(ORDER))){
            orderInfo = map.get(ORDER).toString();
        }

        //构造查询传入之前剔除无用key
        map.remove(JsonToSelectUtil.JOINWAY);
        map.remove(JsonToSelectUtil.JOINFIELD);
        map.remove(JsonToSelectUtil.TABLENAMEKEY);
        map.remove(JsonToSelectUtil.GROUP);
        map.remove(JsonToSelectUtil.CONDITION);
        map.remove(JsonToSelectUtil.ORDER);

        return buildSqlString(map, splitTableName, joinWay, JoinField, conditionInfo, groupInfo, orderInfo);
    }


    private static String buildSqlString(HashMap<String, Object> map, String[] splitTableName, String[] JOINWAY, String[] JOINFIELD, String CONDITIONInfo, String GROUPInfo, String ORDERInfo) {
        SqlBuilder sqlBuilder = new SqlBuilder(new StringBuilder());
        return sqlBuilder.buildSelect(map.keySet()).buildFromTable(splitTableName, JOINWAY, JOINFIELD).buildCondition(CONDITIONInfo).buildGroup(GROUPInfo).buildOrder(ORDERInfo).build();
    }

    public static boolean checkTheKey(Object key){
        if(key == null) return false;
        if(StringUtils.isNullOrEmpty((String)key)) return false;
        return true;
    }

    public static boolean checkTheKey(String[] tableNameArray, String[] firstKey, String[] secondKey){
        if(firstKey.length != secondKey.length || firstKey.length <= 0) return false;
        if((tableNameArray.length != 1 && tableNameArray.length != (firstKey.length + 1)) || tableNameArray.length <= 0) return false;
        return true;
    }
}
